const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    latitude:"",
    longitude:"",
    markers: [],
    mapWidth: '',
    mapHeight: '',
    mapCtx:""
  },
  //获取区域
  getPosition() {
    let _this = this;
    App._get('Area/getPosition', {

    }, res => {
      let data = res.data;
      if(!data.positions.length){
        wx.showToast({
          title: '暂无数据',
          icon: "error"
        })
        return false;
      }
      let markers = data.positions.map(item=>{
         let content = ""
         content = `空码：${item.allNums}个 \n 已赋能：${item.usedNums}个 \n 未赋能： ${item.unUsedNums}个 `
         item.callout = {
              content: content,
              padding: 10,
              display: 'ALWAYS',
              textAlign: 'center',
              borderRadius: 10,
              borderColor: '#ff0000',
              borderWidth: 2,
            }
          item.customCallout = {
            anchorY: 0,
            anchorX: 0,
            display: 'ALWAYS'
          }
         return item;
      })
      _this.setData({
        latitude:data.centerPosition[0],
        longitude:data.centerPosition[1],
        markers:markers
      })
      // 显示所有标记点
      // _this.mapCtx.includePoints({
      //   points: markers,  //放入所有坐标轴的数组   并引用此方法
      //   success: function (e) {
      //     console.log(e)
      //   }
      // })
      _this.mapCtx.getCenterLocation({
        success: function(res){
          console.log(res.longitude)
          console.log(res.latitude)
        }
      })
    }, false, () => {

    })
  },
  onReady: function () {
    this.mapCtx = wx.createMapContext('myMap'); //创建初始地图
  },
  toaddress: function (e) {
    console.log(e)
    var id = e.detail.markerId
    console.log(id)
    // wx.openLocation({
    //   latitude: this.data.markers[id].latitude,
    //   longitude: this.data.markers[id].longitude,
    // })
    // wx.navigateTo({
    //   url: '/pages/index/index',
    //   success: function (res) {},
    //   fail: function (res) {},
    //   complete: function (res) {},
    // })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sy = wx.getSystemInfoSync(),
      mapWidth = sy.windowWidth * 3,
      mapHeight = sy.windowHeight * 2;
    this.setData({
      mapWidth: mapWidth,
      mapHeight: mapHeight
    })
    this.getPosition();
  }
})